package com.taptech.doufu.model.novel

import android.app.Activity
import com.taptech.doufu.app.WeMediaApplication
import com.taptech.doufu.db.history.Read_DataBaseUtil
import com.taptech.doufu.net.retrofit.api.ApiClient
import com.taptech.doufu.net.retrofit.api.RxJavaHelper
import com.taptech.doufu.model.TupleBean
import com.taptech.doufu.model.novel.bean.*
import rx.Observer

class NovelDetailsModel(val activity:Activity) {

    fun getNovelDetail(novelId:Int,keyword:String, observer: Observer<NovelDetailBean>) {
        ApiClient.getInstance().setUseCache(false).service.getNovelDetails(novelId,keyword)
                .map {
                    if (it != null && it.data != null) {
                        val readDataBaseUtil = Read_DataBaseUtil(WeMediaApplication.applicationContext)
                        val record = readDataBaseUtil.queryHistoryInfo(novelId.toString())
                        if (record != null) {
                            it.data.topic.lastReadPosition = record.position
                            if (!record.section_id.isNullOrEmpty()) {
                                it.data.topic.lastReadSectionId = record.section_id.toInt()
                            }
                        }
                        it.data.topic.has_permissions = it.data.has_permissions
                        it.data.topic
                    }else{
                        NovelDetailBean()
                    }
                }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)

    }

    fun getNovelDetail(novelId:Int, observer: Observer<NovelDetailBean>) {
        ApiClient.getInstance().setUseCache(false).service.getNovelDetails(novelId)
            .map {
                if (it != null && it.data != null) {
                    val readDataBaseUtil = Read_DataBaseUtil(WeMediaApplication.applicationContext)
                    val record = readDataBaseUtil.queryHistoryInfo(novelId.toString())
                    if (record != null) {
                        it.data.topic.lastReadPosition = record.position
                        if (!record.section_id.isNullOrEmpty()) {
                            it.data.topic.lastReadSectionId = record.section_id.toInt()
                        }
                    }
                    it.data.topic.has_permissions = it.data.has_permissions
                    it.data.topic
                }else{
                    NovelDetailBean()
                }
            }
            .compose(RxJavaHelper.observeOnMainThread(activity))
            .subscribe(observer)

    }

    fun getNovelShareRank(novelId: Int,observer:Observer<NovelShareRankGson>) {
        ApiClient.getInstance().service.getNovelShareRank(novelId)
                .map {
                    it.data
                }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)
    }


    fun getRecommendNovel(uid:Int,novelId: Int,size:Int,begin:Int,observer:Observer<NovelRecommendGson>) {
        ApiClient.getInstance().service.getNovelRecommend(uid,novelId,size,begin)
                .map {
                    it.data
                }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)

    }

    fun getNovelBookList(novelId: Int,observer:Observer<NovelBookListGson>){
        ApiClient.getInstance().service.getNovelBookList(novelId)
                .map { it.data }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)
    }

    fun attentionUser(userId: Int,observer:Observer<NovelAttentionBean>) {
        ApiClient.getInstance().service.attentionUser(userId)
                .map {
                    it.data
                }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)
    }

    fun cancelAttentionUser(userId: Int,observer: Observer<Boolean>){
        ApiClient.getInstance().service.cancelAttentionUser(userId)
                .map {
                    it.data == "ok"
                }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)
    }

    fun praiseComment(id: Int,observer: Observer<Boolean>) {
        ApiClient.getInstance().service.praiseComment(id)
                .map {
                    !it.data.res.isNullOrEmpty()
                }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)
    }

    fun cancelPraiseComment(id: Int, observer: Observer<Boolean>) {
        ApiClient.getInstance().service.cancelPraiseComment(id)
                .map {
                    !it.data.res.isNullOrEmpty()
                }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)
    }


    fun collectNovel(uid: Int, objectType: Int, id: Int,observer: Observer<Boolean>) {
        ApiClient.getInstance().service.collectNovel(uid,objectType,id)
                .map {
                    it.status == 0
                }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)
    }

    fun cancelCollectNovel(uid: Int, objectType: Int, id: Int, observer: Observer<Boolean>) {
        ApiClient.getInstance().service.cancelCollectNovel(uid,objectType,id)
                .map { it.status == 0 }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)
    }

    fun getNovelChapterList(novelId: Int,observer: Observer<TupleBean<Float,Boolean>>) {
        ApiClient.getInstance().service.getNovelChapterList(novelId)
                .map {
                    if (it.data != null && it.data.list!=null) {
                        var totalPrice = 0f
                        var hasPaid = false
                        it.data.list.forEach { it1 ->
                            if (it1.need_pay && it1.vip > 0 && it1.price > 0) {
                                totalPrice+=it1.price
                            }
                            if (it1.vip>0 && !it1.need_pay){
                                hasPaid = true
                            }
                        }
                        TupleBean(totalPrice,hasPaid)
                    }else{
                        TupleBean(0f,false)
                    }
                }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)
    }


    fun buyTheBook(novelId: Int,objectType: Int,observer:Observer<NovelBuyBean>) {
        ApiClient.getInstance().service.buyTheBook(novelId, objectType)
                .map {
                    val data = it.data
                    data.user_msg = it.user_msg
                    data
                }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)
    }

    fun buyAllChapter(novelId: Int,objectType: Int,observer:Observer<NovelBuyBean>) {
        ApiClient.getInstance().service.buyAllChapter(novelId, objectType)
                .map {
                    val data = it.data
                    data.user_msg = it.user_msg
                    data
                }
                .compose(RxJavaHelper.observeOnMainThread(activity))
                .subscribe(observer)
    }
}
